﻿using UnityEngine;

namespace Learning
{
    /// <summary>
    /// 绘制出摄像机在地面(y=0)的可视区域
    /// 摄像机y轴必须高于地面且x轴旋转为30到90度之间，yz轴旋转必须为0
    /// </summary>
    [ExecuteInEditMode]
    public class CameraViewport : MonoBehaviour
    {
        private static Ray ray;
        private static Vector3 dir;
        private static Vector3 move;
        private static Vector3 point;
        private static Vector2 v2;

        public static Vector3 ScreenPointToGroundPoint(Camera camera, float x, float y)
        {
            v2.Set(x, y);
            ray = camera.ScreenPointToRay(v2);
            dir = ray.direction / ray.direction.y;
            move = ray.origin.y * dir;
            point = ray.origin - move;
            point.y = 0;
            return point;
        }

        public static void GetViewport(Camera camera, Vector3[] viewportPos)
        {
            // 矩形/梯形左上角
            viewportPos[0] = ScreenPointToGroundPoint(camera, 0, Screen.height);
            // 矩形/梯形右上角
            viewportPos[1] = ScreenPointToGroundPoint(camera, Screen.width, Screen.height);
            // 梯形左下角
            viewportPos[2] = ScreenPointToGroundPoint(camera, 0, 0);
            // 梯形右下角
            viewportPos[3] = ScreenPointToGroundPoint(camera, Screen.width, 0);
            // 矩形左下角
            viewportPos[4].Set(viewportPos[0].x, 0, viewportPos[2].z);
            // 矩形右下角
            viewportPos[5].Set(viewportPos[1].x, 0, viewportPos[3].z);
            // 屏幕中心点
            viewportPos[6] = ScreenPointToGroundPoint(camera, Screen.width / 2f, Screen.height / 2f);
            // 空间中心点
            viewportPos[7] = (viewportPos[0] + viewportPos[5]) / 2f;
        }

        public Camera camera;

        private Vector3[] _viewportPos = new Vector3[8];

        void Update()
        {
            GetViewport(camera, _viewportPos);

            // 梯形
            Debug.DrawLine(_viewportPos[0], _viewportPos[1], Color.green);
            Debug.DrawLine(_viewportPos[1], _viewportPos[3], Color.green);
            Debug.DrawLine(_viewportPos[3], _viewportPos[2], Color.green);
            Debug.DrawLine(_viewportPos[2], _viewportPos[0], Color.green);

            // 矩形
            Debug.DrawLine(_viewportPos[0], _viewportPos[1], Color.yellow);
            Debug.DrawLine(_viewportPos[1], _viewportPos[5], Color.yellow);
            Debug.DrawLine(_viewportPos[5], _viewportPos[4], Color.yellow);
            Debug.DrawLine(_viewportPos[4], _viewportPos[0], Color.yellow);
            
            // 屏幕中心点
            var screenCenter = _viewportPos[6];
            Debug.DrawLine(screenCenter + new Vector3(-3, 0, 0), screenCenter + new Vector3(3, 0, 0), Color.cyan);
            Debug.DrawLine(screenCenter + new Vector3(0, 0, -3), screenCenter + new Vector3(0, 0, 3), Color.cyan);
            
            // 空间中心点
            var spaceCenter = _viewportPos[7];
            Debug.DrawLine(spaceCenter + new Vector3(-3, 0, 0), spaceCenter + new Vector3(3, 0, 0), Color.magenta);
            Debug.DrawLine(spaceCenter + new Vector3(0, 0, -3), spaceCenter + new Vector3(0, 0, 3), Color.magenta);
        }
    }
}
